package com.baidu.lib_home.activity


import android.Manifest
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.baidu.lib_audio.AudioController
import com.baidu.lib_audio.service.MusicService
import com.baidu.lib_common.base.BaseActivity
import com.baidu.lib_common.constant.NetWorkAvailableEvent
import com.baidu.lib_common.constant.NetWorkLostEvent
import com.baidu.lib_common.utils.PermissionsUtils
import com.baidu.lib_common.utils.dp2px
import com.baidu.lib_home.databinding.ActivityMainBinding
import com.baidu.lib_leancloud.LeanCloudManager
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainActivity : BaseActivity<ActivityMainBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    override fun initUI() {
        super.initUI()
        PermissionsUtils.instance.initPermissionsUtils(this)

        val launcher = registerForActivityResult(
            ActivityResultContracts.RequestPermission(),
            {granted ->
                if(!granted){
                    showDialog("请设置权限以更好的使用应用")
                }else{
                    startService()
                }
            }
        )
        questPermission(launcher)


    }
    fun questPermission(launcher: ActivityResultLauncher<String>){
        if(ContextCompat.checkSelfPermission(this,
                Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED
            ){
            launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
        }else{
            startService()
        }
    }
    private fun showDialog(message: String) {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("友情提示！")
        builder.setMessage(message)
        builder.setPositiveButton("立即设置") { dialog: DialogInterface?, which: Int ->
            dialog!!.dismiss()
            // 添加包名，确保跳转到当前应用的设置页面
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
                data = Uri.fromParts("package", packageName, null)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            startActivity(intent)
        }
        builder.setNegativeButton(
            "取消"
        ) { dialog: DialogInterface?, which: Int -> }
        val dialog = builder.create()
        dialog.show()
    }
    private fun startService(){
        val intent = Intent(
            this,
            MusicService::class.java
        )
        startService(intent) // 先启动服务
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun OnNetworkAvailableEvent(event: NetWorkAvailableEvent){
        LeanCloudManager.instance.changeIsNetWork(true)
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun OnNetWorkLostEvent(event: NetWorkLostEvent){
        LeanCloudManager.instance.changeIsNetWork(false)
        AudioController.instance.pause()
        Toast.makeText(this,"请检查网络设置,需要网络才能正常运行应用", Toast.LENGTH_LONG).show()
    }
}